package lumis.service.portalmanagement.servicesearchindex;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import lumis.doui.search.common.DouiIndexerFactory;
import lumis.portal.PortalException;
import lumis.portal.controller.ControllerException;
import lumis.portal.controller.ControllerXml;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.ITransaction;
import lumis.util.XmlUtil;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Controller responsible for reindexing contents of a doui service instance 
 *
 * @version $Revision: 10458 $ $Date: 2009-06-02 15:49:09 -0300 (Tue, 02 Jun 2009) $
 * @since 4.0.7
 */
public class DouiReindexerControllerXml extends ControllerXml
{
	protected Document requestXmlDoc = null; 
	private static ILogger logger = LoggerFactory.getLogger(DouiReindexerControllerXml.class);

	public DouiReindexerControllerXml(HttpServletRequest request, HttpServletResponse response, PageContext pageContext) throws ControllerException, PortalException
	{
		super(request, response);
	}

	public void handleRequest() throws ControllerException, Exception
	{
		try
		{
			String method = null;
            ITransaction portalTransaction = PortalTransactionFactory.createTransaction();
            try
            {
            	portalTransaction.begin();
            	
				requestXmlDoc = XmlUtil.getDocument(request);
				loadUserInfo(requestXmlDoc, portalTransaction);
				
				Node methodNode = XmlUtil.selectSingleNode("request/method", requestXmlDoc);
	
				if(methodNode != null)
				{
					method = XmlUtil.readAttributeString("name", methodNode);
				}
				
				if(method == null)
				{
					throw new ControllerException("STR_METHOD_NOT_SPECIFIED");
				}
				else if (method.equals("indexServiceInstance"))
				{
					indexServiceInstance(portalTransaction);
				}
				else
				{
					throw new ControllerException("STR_INVALID_METHOD");
				}
				
				portalTransaction.commit();
            }
            catch(Exception e)
            {
            	portalTransaction.rollback();
            	throw e;
            }
			finally
			{
				portalTransaction.dispose();
			}
		}
		catch(PortalException e)
		{
			out.write(getResponseXml(e));
		}
		catch(Exception e)
		{
			PortalException portalException = new PortalException(e.getMessage(), e);
			out.write(getResponseXml(portalException));
		}
	}
	
	/**
	 * Indexes service instances.
	 * @param transaction portal transaction.
	 * @since 4.2.2
	 */
	private void indexServiceInstance(ITransaction transaction) throws ControllerException, PortalException
	{
		Node methodNode = XmlUtil.selectSingleNode("request/method", requestXmlDoc);
		String serviceInstanceId = XmlUtil.readNodeString("id", methodNode);
		logger.debug("DouiServiceInstanceIndexControllerXml Begin serviceInstanceId='" + serviceInstanceId + "'");
		DouiIndexerFactory.getDouiIndexer().indexServiceInstance(sessionConfig, serviceInstanceId, transaction);
		logger.debug("DouiServiceInstanceIndexControllerXml Done serviceInstanceId='" + serviceInstanceId + "'");
	}
}
